package cn.com.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import cn.com.entity.Consumer;
import cn.com.entity.Departments;
import cn.com.entity.Doctor;
import cn.com.entity.Registerform;

public class RegisterformDao {
	//挂号单的dao文件
	private SessionFactory sessionFac;
	public SessionFactory getSessionFac() {
		return sessionFac;
	}
	public void setSessionFac(SessionFactory sessionFac) {
		this.sessionFac = sessionFac;
	}
	Session session;
	Transaction ta;
	//查询所有挂号单
	public List<Registerform> selectAllRegisterform(){
		session=sessionFac.openSession();
		Query query=session.createQuery("from Registerform");
		@SuppressWarnings("unchecked")
		List<Registerform> list=query.list();
		System.out.println(list);
		return list;
	}
	//通过医生id查询挂号单
	public List<Registerform> selectRegisterformByDoctorid(Doctor doctor){
		session=sessionFac.openSession();
		Query query=session.createQuery("from Registerform where doctorid=?");
		query.setInteger(0, doctor.getDoctorid());
		@SuppressWarnings("unchecked")
		List<Registerform> list=query.list();
		return list;
	}
	//通过科室id查询挂号单
		public List<Registerform> selectRegisterformByDepartmentsid(Departments departments){
			session=sessionFac.openSession();
			Query query=session.createQuery("from Registerform where departmentsid=?");
			query.setInteger(0, departments.getDepartmentsid());
			@SuppressWarnings("unchecked")
			List<Registerform> list=query.list();
			return list;
		}
		//通过用户id查询挂号单
				public List<Registerform> selectRegisterformByConsumerid(Consumer consumer){
					session=sessionFac.openSession();
					Query query=session.createQuery("from Registerform where consumerid=?");
					query.setInteger(0, consumer.getConsumerid());
					@SuppressWarnings("unchecked")
					List<Registerform> list=query.list();
				
					return list;
				}
			//添加挂号单
				public boolean addRegisterform(Registerform registerform){
					boolean bool=false;
					session=sessionFac.openSession();
					ta=session.beginTransaction();
					try {
						//添加系统当前时间
						SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");   
						String date=sdf.format(new Date());  
						registerform.setRegistertime(date);
						session.save(registerform);
						ta.commit();
						session.close();
						bool=true;
						return bool;
					} catch (HibernateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return  bool;
					}
				}
			//删除挂号单	
				public boolean deleteRegisterform(Registerform registerform){
					boolean bool=false;
					session=sessionFac.openSession();
					ta=session.beginTransaction();
					try {
						session.delete(registerform);
						session.close();
						bool=true;
						return bool;
					} catch (HibernateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return  bool;
					}
				}
			//更新挂号单
				public boolean updateRegisterform(Registerform registerform){
					boolean bool=false;
					session=sessionFac.openSession();
					ta=session.beginTransaction();
					try {
						session.update(registerform);
						session.close();
						bool=true;
						return bool;
					} catch (HibernateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return  bool;
					}
				}	
				
}
